package day12;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: 王丹
 * @create: 2021-07-05 12:29
 * ReentrantLock基本使用
 **/
public class Demo3_1 {

    private static int num = 0;
    private static ReentrantLock lock = new ReentrantLock(false);

    public static  void add() {
        num++;
    }

    public static class T extends Thread {
        public void run() {
            try {
                lock.lock();
                for (int i = 0; i < 1000; i++) {
                    add();
                }
            } finally {
              lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        T t1 = new T();
        T t2 = new T();
        T t3 = new T();

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

        System.out.println(num);
    }
}
